package com.example.nailingsignin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@RestController
public class signinController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String prefix = "Signin_*";
    /*
        用户签到
     */
    @GetMapping("/Signin")
    public Map userSign(String name ,Integer sex,String grade){
        Map result = new HashMap();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = df.format(new Date());
        String redisKey = "Signin_"+name+sex+grade+"_"+date;
        if (redisTemplate.hasKey(redisKey)){
            result.put("status",1);
            result.put("msg","你已完成签到");
            return result;
        }
        redisTemplate.opsForValue().set(redisKey,"100",60*20, TimeUnit.SECONDS);
        result.put("status",2);
        result.put("msg","签到成功");
        return result;
    }
    /*
    查看签到的人
     */
    @GetMapping("/find")
    public Map findSign(){
        Map result = new HashMap();
        Set keys = redisTemplate.keys(prefix);
        result.put("data",keys);
        return result;
    }

}
